// pages/mine/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        topBarHeight: 64,
        userInfo:null,
        appoints:0,
        nums:{nopay:0,appoints:0,finish:0,refunds:0,cancel:0}
        
    },
    getBarInfo(e) {
        this.setData({
            topBarHeight: e.detail.topBarHeight
        })
    },
    gologout(){
        var that =this;
       wx.showModal({
         title: '提示',
         content: '是否退出登录？',
         complete: (res) => {
           if (res.cancel) {
             
           }
       
           if (res.confirm) {
            request.requestLoading(
                "/api/user/logout",
                {
                 
                },
                'POST',
              ).then((res) => {
                  console.log(res)
                  if(res.code==1){
                      wx.showToast({
                        title: res.msg,
                        icon:'none'
                      })
                      that.setData({
                          userInfo:null
                      })
                      wx.setStorageSync('token','');
                      wx.setStorageSync('userInfo','');
                    
                      console.log(wx.getStorageSync('userInfo'))
                     setTimeout(function(){
                        app.init_userInfo(true).then((res) => {
                            if(res){
                              wx.setStorageSync('userInfo', res)
                              that.setData({
                              userInfo: res
                            })
                            }else{
                                that.setData({
                                    userInfo: null
                                  }) 
                            }
                          });
                     },1000)
                      that.getOrderNums();
                      that.getMine();
                  }else{
                      wx.showToast({
                        title: res.msg,
                        icon:'none'
                      })
                      app.init_userInfo(true).then((res) => {
                        if(res){
                          wx.setStorageSync('userInfo', res)
                          that.setData({
                          userInfo: res
                        })
                        }else{
                            that.setData({
                                userInfo: null
                              }) 
                        }
                      });
                      that.getOrderNums();
                      that.getMine();
                  }
               
          
              }).catch((err) => {
                console.error(err);
              });
           }
         }
       })
    },
    gorights(){
       if(this.checkLogin()){
           wx.navigateTo({
             url: '/pages/my_rights/index',
           })
       }
    },
    goorders(e){
        if(this.checkLogin()){
            var  index = e.currentTarget.dataset.index;
            wx.navigateTo({
              url: '/pages/my_orders/index?tab='+index,
            })
        }
    },
    goappoint(){
        if(this.checkLogin()){
            wx.navigateTo({
              url: '/pages/my_appointment/index',
            })
        }
    },
    gologoin(){
        wx.navigateTo({
          url: '/pages/login/index',
        })
       },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    checkLogin(){
        if(this.data.userInfo){
            
            return true
        }else{
            wx.showModal({
              title: '提示',
              content: '您还未登录，去登录',
              complete: (res) => {
                if (res.cancel) {
                    return false;
                }
            
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/login/index',
                  })
                }
              }
            })
            return false;
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log('1111234567890')
        var that =this;
       
        if(!wx.getStorageSync('token')){
            var that=this
           setTimeout(function(){
            app.init_userInfo(true).then((res) => {
              
                wx.setStorageSync('userInfo', res)
                that.setData({
                userInfo: res
              })
              });
        
           },1000)
          }else{
            app.init_userInfo(false).then((res) => {
               wx.setStorageSync('userInfo', res)
               this.setData({
               userInfo: res
             })
              });
              
          }
      
          console.log(that.data.userInfo)
          this.getOrderNums();
          this.getMine();
    },
    getMine(){
        var that =this;
        request.requestLoading(
            "/api/login/getMorders",
            {
             
            },
            'POST',
          ).then((res) => {
         that.setData({
            appoints:res.data.appoints,
            benefits:res.data.benefits,
         })
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    getOrderNums(){
        var that =this;
        request.requestLoading(
            "/api/order/getOrderNums",
            {},
            'POST',
          ).then((res) => {
            that.setData({
                nums:res.data
            })
          
           
           
      
          }).catch((err) => {
            console.error(err);
          });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        app.init_userInfo(true).then((res) => {
            if(res){
              wx.setStorageSync('userInfo', res)
              this.setData({
              userInfo: res
            })
            }
          });
          this.getOrderNums();
          this.getMine();
        setTimeout(function(){
          wx.stopPullDownRefresh();
        },500)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
    handleOpenPrivacyContract() {
        // 打开隐私协议页面
        wx.navigateTo({
          url: '/pages/policy/index',
        })
        
      },
      gomyinfo(){
       if(this.checkLogin()){
           wx.navigateTo({
             url: '/pages/choose_patient/index',
           })
       }
      },
      goinfo(){
        if(this.checkLogin()){
            wx.navigateTo({
              url: '/pages/edit_info/index',
            })
        }
      },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})